<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>令牌管理</title>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <style>
    body { font-family: Arial, sans-serif; background:#0b132b; color:#e0e0e0; }
    header { padding:16px; background:#1c2541; }
    .container { padding:16px; }
    table { width:100%; border-collapse:collapse; margin-top:12px; }
    th, td { border:1px solid #3a506b; padding:8px; }
    th { background:#1c2541; }
    input, button { padding:8px; border-radius:6px; border:1px solid #3a506b; background:#0b132b; color:#e0e0e0; }
    button { background:#5bc0be; color:#0b132b; font-weight:700; cursor:pointer; }
    .row { display:flex; gap:8px; margin-top:12px; }
    .warn { color:#ffb703; }
  </style>
</head>
<body>
  <header style="display:flex;justify-content:space-between;align-items:center;">
    <h2>令牌管理</h2>
    <a href="/api/quant/admin/home" style="color:#5bc0be;text-decoration:none;">返回首页</a>
  </header>
  <div class="container">
    <div class="row">
      <input id="remark" placeholder="备注（默认：未命名客户端+数字）" />
      <button onclick="createToken()">创建令牌</button>
      <span id="msg" class="warn"></span>
    </div>
    <table>
      <thead>
        <tr>
          <th>令牌</th><th>备注</th><th>启用</th><th>创建时间</th><th>操作</th>
        </tr>
      </thead>
      <tbody id="list"></tbody>
    </table>
  </div>
  <script>
    async function load(){
      const r = await fetch('/api/quant/admin/tokens');
      if(!r.ok){ document.getElementById('msg').textContent='认证失败或网络错误'; return; }
      const j = await r.json();
      const tbody = document.getElementById('list');
      tbody.innerHTML = '';
      (j.items||[]).forEach(item => {
        const tr = document.createElement('tr');
        tr.innerHTML = `
          <td><code>${item.token}</code></td>
          <td>
            <input value="${item.remark}" />
            <button onclick="updateRemark('${item.token}', this.previousElementSibling.value)">保存</button>
          </td>
          <td>${item.active?'启用':'停用'}</td>
          <td>${item.created_at||''}</td>
          <td>
            <button onclick="toggleActive('${item.token}', ${!item.active})">${item.active?'停用':'启用'}</button>
            <button onclick="del('${item.token}')">删除</button>
            <button onclick="hardDel('${item.token}')">彻底删除</button>
          </td>
        `;
        tbody.appendChild(tr);
      });
    }
    async function createToken(){
      const remark = document.getElementById('remark').value.trim();
      const r = await fetch('/api/quant/admin/token', { method:'POST', headers:{'Content-Type':'application/json'}, body:JSON.stringify({ remark:remark||undefined }) });
      if(r.ok){ document.getElementById('msg').textContent='创建成功'; load(); } else { document.getElementById('msg').textContent='创建失败'; }
    }
    async function del(tok){
      if(!confirm('确认删除令牌？')) return;
      const r = await fetch(`/api/quant/admin/token/${tok}`, { method:'DELETE' });
      if(r.ok){ load(); } else { alert('删除失败'); }
    }
    async function hardDel(tok){
      const input = prompt('请输入要删除的令牌以确认：');
      if(!input || input.trim() !== tok){ alert('令牌不匹配，已取消'); return; }
      const r = await fetch(`/api/quant/admin/token/${tok}?hard=1`, { method:'DELETE' });
      if(r.ok){ load(); } else { alert('彻底删除失败'); }
    }
    async function updateRemark(tok, remark){
      const r = await fetch(`/api/quant/admin/token/${tok}/remark`, { method:'PATCH', headers:{'Content-Type':'application/json'}, body:JSON.stringify({ remark }) });
      if(r.ok){ document.getElementById('msg').textContent='备注已更新'; } else { document.getElementById('msg').textContent='更新失败'; }
    }
    async function toggleActive(tok, active){
      const r = await fetch(`/api/quant/admin/token/${tok}/active`, { method:'PATCH', headers:{'Content-Type':'application/json'}, body:JSON.stringify({ active }) });
      if(r.ok){ load(); } else { alert('操作失败'); }
    }
    load();
  </script>
</body>
</html>